package com.hit.userManageService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hit.JwtUtils;
import com.hit.commonUtils.MD5;
import com.hit.serviceBase.exceptionHandler.MyException;
import com.hit.userManageService.entity.UcenterMember;
import com.hit.userManageService.entity.vo.RegisterVo;
import com.hit.userManageService.mapper.UcenterMemberMapper;
import com.hit.userManageService.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-11-14
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember member) {
        //获取手机号和密码
        String mobile=member.getMobile();
        String password=member.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new MyException(20001,"手机或密码为空，登陆失败");
        }
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember=baseMapper.selectOne(wrapper);
        if(mobileMember==null){
            throw new MyException(20001,"该手机号尚未注册，请先注册");
        }
        if(!mobileMember.getPassword().equals(MD5.encrypt(password))){
            throw new MyException(20001,"密码错误");
        }
        if(mobileMember.getIsDisabled()){
            throw new MyException(20001,"用户已经被封号");
        }
        //登陆成功，根据用户的id和name生成token
        String token= JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());
        return token;
    }

    @Override
    public void registerUser(RegisterVo registerVo) {//获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new MyException(20001,"注册失败");
        }

        //获取redis中的手机号对应的验证码
        String redisCode=  redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new MyException(20001,"验证码输入错误");
        }
        //判断手机号是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new MyException(20001,"该手机号已经被注册");
        }

        //添加到数据库中
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));//密码需要加密的
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("https://my-file001.oss-cn-beijing.aliyuncs.com/2022/11/07/86e9a86da9b8487285a60e4e89b5577b1.jpg");
        baseMapper.insert(member);
    }
}
